package com.example.asset.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.asset.entity.AssetInformation;
import com.example.asset.entity.AssetMaintain;
import com.example.asset.mapper.AssetMaintainMapper;
import com.example.asset.utils.UtilRandom;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequiredArgsConstructor
@Service
public class AssetMaintainService extends ServiceImpl<AssetMaintainMapper, AssetMaintain>{


    private final AssetInformationService assetInformationService;

    private final AssetMaintainMapper assetMaintainMapper;

    /**
     * 保存添加的资产维修信息
     *
     * @param assetMaintain
     * @return
     */
    public boolean saveAssetMaintain(AssetMaintain assetMaintain){
        assetMaintain.setMaintainRecord(generateUniqueSerialNumber());
        AssetInformation asset = assetInformationService.selectAssetNum(assetMaintain.getAssetNumber());
        asset.setAssetState("维修");
        return saveOrUpdate(assetMaintain) && assetInformationService.saveOrUpdate(asset);
    }

    /**
     * 更新维修结果
     *
     * @param assetMaintain
     * @param result
     * @return
     */
    public boolean updateMaintainResult(AssetMaintain assetMaintain, String result){

        AssetInformation asset = assetInformationService.selectAssetNum(assetMaintain.getAssetNumber());
        asset.setAssetState(result);
        AssetMaintain maintain = findMaintainRecode(assetMaintain);
        maintain.setResult(result);
        maintain.setMaintenance(assetMaintain.getMaintenance());
        return saveOrUpdate(maintain) && assetInformationService.saveOrUpdate(asset);
    }

    /**
     * 生成维修流水号
     *
     * @return
     */
    private String generateUniqueSerialNumber(){
        String serialNumber;
        boolean isUnique = false;

        Pattern pattern = Pattern.compile("\\d{14}");
        Matcher matcher;

        do{
            // 生成一个14位的随机数
            serialNumber = UtilRandom.generateRandomNumber();
            // 匹配正则表达式
            matcher = pattern.matcher(serialNumber);

            // 检查数据库是否存在相同的资产编号
            QueryWrapper<AssetMaintain> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("maintain_record", serialNumber);
            long count = this.count(queryWrapper);

            // 如果数据库中没有相同的资产编号，流水号是唯一的
            if(matcher.matches() && count == 0){
                isUnique = true;
            }
        } while(!isUnique);

        return serialNumber;
    }

    public AssetMaintain findMaintainRecode(AssetMaintain assetMaintain){
        // 检查数据库是否存在相同的资产编号
        QueryWrapper<AssetMaintain> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("maintain_record", assetMaintain.getMaintainRecord());
        AssetMaintain maintain = assetMaintainMapper.selectOne(queryWrapper);
        return maintain;
    }

    public Long maintainCount(){
        QueryWrapper<AssetMaintain> queryWrapper = new QueryWrapper<>();
        return this.count(queryWrapper);
    }

}